
package jonathanfinerty.onceexample.slice;

import jonathanfinerty.once.Once;
import jonathanfinerty.onceexample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;

import java.util.concurrent.TimeUnit;

import static jonathanfinerty.once.Amount.exactly;
import static jonathanfinerty.once.Once.*;

public class MainAbilitySlice extends AbilitySlice {
    private static final String SHOW_NEW_SESSION_DIALOG = "NewSessionDialog";
    private static final String SHOW_FRESH_INSTALL_DIALOG = "FreshInstallDialog";
    private static final String SHOW_NEW_VERSION_DIALOG = "NewVersionDialog";
    private static final String SHOW_MINUTE_DIALOG = "OncePerMinuteDialog";
    private static final String SHOW_SECOND_DIALOG = "OncePerSecondDialog";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        markDone("Application Launched");

        Button oncePerSessionButton = (Button) findComponentById(ResourceTable.Id_once_per_session_button);
        oncePerSessionButton.setClickedListener(new Component.ClickedListener() {
            @Override
           public void onClick(Component component) {
                if (!beenDone(Once.THIS_APP_SESSION, SHOW_NEW_SESSION_DIALOG)) {
                    showDialog("This dialog should only appear once per app session");
                    markDone(SHOW_NEW_SESSION_DIALOG);
                }
            }
        });

        Button oncePerInstallButton = (Button) findComponentById(ResourceTable.Id_once_per_install_button);
        oncePerInstallButton.setClickedListener(new Component.ClickedListener() {
            @Override
           public void onClick(Component component) {
                if (!beenDone(THIS_APP_INSTALL, SHOW_FRESH_INSTALL_DIALOG)) {
                    showDialog("This dialog should only appear once per app installation");
                    markDone(SHOW_FRESH_INSTALL_DIALOG);
                }
            }
        });

        Button oncePerVersionButton = (Button) findComponentById(ResourceTable.Id_once_per_version_button);
        oncePerVersionButton.setClickedListener(new Component.ClickedListener() {
            @Override
           public void onClick(Component component) {
                if (!beenDone(Once.THIS_APP_VERSION, SHOW_NEW_VERSION_DIALOG)) {
                    showDialog("This dialog should only appear once per app version");
                    markDone(SHOW_NEW_VERSION_DIALOG);
                }
            }
        });

        Button oncePerMinuteButton = (Button) findComponentById(ResourceTable.Id_once_per_minute_button);
        oncePerMinuteButton.setClickedListener(new Component.ClickedListener() {
            @Override
           public void onClick(Component component) {
                if (!beenDone(TimeUnit.MINUTES, 1, SHOW_MINUTE_DIALOG)) {
                    showDialog("This dialog should only appear once per minute");
                    markDone(SHOW_MINUTE_DIALOG);
                }
            }
        });

        Button oncePerSecondButton = (Button) findComponentById(ResourceTable.Id_once_per_second_button);
        oncePerSecondButton.setClickedListener(new Component.ClickedListener() {
            @Override
           public void onClick(Component component) {
                if (!beenDone(1000L, SHOW_SECOND_DIALOG)) {
                    showDialog("This dialog should only appear once per second");
                    markDone(SHOW_SECOND_DIALOG);
                }
            }
        });

        Button oncePerThreePressesButton = (Button) findComponentById(ResourceTable.Id_three_presses_button);
        oncePerThreePressesButton.setClickedListener(new Component.ClickedListener() {
            @Override
           public void onClick(Component component) {
                String buttonPressedTag = "button pressed";
                markDone(buttonPressedTag);
                if (beenDone(buttonPressedTag, exactly(3))) {
                    showDialog("This dialog should only appear once every three presses");
                    clearDone(buttonPressedTag);
                }
            }
        });

        Button resetButton = (Button) findComponentById(ResourceTable.Id_reset_all_button);
        resetButton.setClickedListener(new Component.ClickedListener() {
            @Override
           public void onClick(Component component) {
                Once.clearAll();
            }
        });
    }

    private void showDialog(String s) {
        ToastDialog toastDialog = new ToastDialog(getContext());
        toastDialog.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        toastDialog.setText(s);
        Text toastText=(Text) toastDialog.getComponent();
        if(toastText!=null)
        {
            toastText.setMultipleLine(true);
            toastText.setTextSize(14, Text.TextSizeType.FP);
            toastText.setPadding(20, 20, 20, 50);
        }
        toastDialog.show();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
